<template>
  <div class='setting-container'>
    <el-card>
      <!-- 导航 -->
      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>个人设置</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
      <!-- 栅格 -->
      <el-row>
        <el-col :span="12">
          <!-- 表单 -->
          <el-form label-width="120px">
            <el-form-item label="编号：">{{userInfo.id}}</el-form-item>
            <el-form-item label="手机：">{{userInfo.mobile}}</el-form-item>
            <el-form-item label="媒体名称：">
              <el-input v-model="userInfo.name"></el-input>
            </el-form-item>
            <el-form-item label="媒体简介：">
              <el-input v-model="userInfo.intro" type="textarea" :rows="3"></el-input>
            </el-form-item>
            <el-form-item label="联系邮箱：">
              <el-input v-model="userInfo.email"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="hamendUserInfo" type="primary">保存设置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <!-- 上传组件 action必须属性-->
          <el-upload
            class="avatar-uploader"
            action="http://ttapi.research.itcast.cn/mp/v1_0/user/photo"
            :http-request="uploadPhoto"
            :show-file-list="false">
            <img v-if="userInfo.photo" :src="userInfo.photo" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="edit-photo">修改头像</p>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { getUserInfo, amendUserInfo, amendUserPhoto } from '@/api/user'
export default {
  name: 'my-setting',
  data () {
    return {
      // 用户信息
      userInfo: {
        name: '',
        intro: '',
        email: '',
        mobile: '',
        id: '',
        photo: ''
      },
      // 支持上传组件的数据
      imageUrl: null
    }
  },
  methods: {
    uploadPhoto (fileObj) {
      const { file } = fileObj
      const formData = new FormData()
      formData.append('photo', file)
      amendUserPhoto(formData).then(res => {
        // 将成功返回的图片url在用户头像的位置显示出来
        this.userInfo.photo = res.data.data.photo
        // 在这里修改头像成功之后将最新的头像数据传递到layout中进行修改
        this.eventBus.$emit('set-photo', this.userInfo.photo)
      })
    },
    // 获取用户信息
    hgetUserInfo () {
      getUserInfo().then(res => {
        this.userInfo = res.data.data
      })
    },
    // 修改用户信息
    hamendUserInfo () {
      const data = {
        name: this.userInfo.name,
        intro: this.userInfo.intro,
        email: this.userInfo.email
      }
      amendUserInfo(data).then(res => {
        // 在这里修改名字成功后将最新的名字数据传递到layout中进行修改
        this.eventBus.$emit('set-name', this.userInfo.name)
      })
    }
  },
  mounted () {
    this.hgetUserInfo()
  }
}
</script>

<style scoped lang='less'>
  .avatar{
    width: 178px;
    height: 178px;
    line-height: 178px;
  }
  .avatar-uploader {
      text-align: center;
  }
  .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
  }
  .avatar-uploader /deep/ .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
  }
.edit-photo{
  font-size: 12px;
  text-align: center;
}
</style>
